package com.wantup.miaosha.controller;

import com.wantup.miaosha.entity.TblUser;
import com.wantup.miaosha.pojo.GoodsPojo;
import com.wantup.miaosha.service.GoodsService;
import com.wantup.miaosha.service.UserService;
import com.wantup.miaosha.utils.DateAndStringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Zhaoluowei
 * @version 1.0
 * @date 2019/6/5 15:42
 */
@Controller
@RequestMapping(value = "/goods")
@Slf4j
public class GoodsController {

    @Autowired
    private UserService userService;

    @Autowired
    private GoodsService goodsService;


    ////////////////////////////////////////////////业务逻辑 ////////////////////////////////////////////

    /**
     * session的有效期是 最后一次访问 时间  加上办个小时。
     *
     * @param modelMap
     * @param cookie
     * @param paramToken
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/toGoodsList")
    public String toGoodsList(ModelMap modelMap,
                              @CookieValue(value = "token", required = false) String cookie,
                              @RequestParam(value = "token") String paramToken) throws Exception {
        if (StringUtils.isEmpty(cookie) || StringUtils.isEmpty(paramToken)) {
            return "login";
        }
        log.info("cookie -->  " + cookie);
        log.info("paramToken -->  " + paramToken);
        String token = StringUtils.isEmpty(paramToken) ? cookie : cookie;
        log.info("传入的值 --> token " + token);
        TblUser tblUser = this.userService.getUserByTokenService(token);

        modelMap.addAttribute("wc", tblUser.getName());
        return "goods_list";
    }

    /**
     * 优化后的第二版
     *
     * @param modelMap
     * @param tblUser
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/toGoodsList2")
    public String toGoodsList2(ModelMap modelMap, TblUser tblUser) throws Exception {
        //查询商品列表
        List<GoodsPojo> goodsListVo = goodsService.getGoodsListVo();
        log.info("获取秒杀商品信息 --> goodsListVo " + goodsListVo);

        modelMap.addAttribute("wc", tblUser.getName());
        modelMap.addAttribute("goodsList", goodsListVo);
        return "goods_list";
    }

    @RequestMapping(value = "/to_detail/{id}")
    public String toGoodsdetail(ModelMap modelMap,@PathVariable("id") String id) throws Exception {

        //modelMap.addAttribute("user", user);
        GoodsPojo goods = this.goodsService.getGoodsInfoByGoodsId(id);
        modelMap.addAttribute("goods", goods);

        //获取秒杀开始时间  结束时间
        long start = DateAndStringUtil.StringToDate3(goods.getStartDate()).getTime();
        long end = DateAndStringUtil.StringToDate3(goods.getEndDate()).getTime();
        long currentTimeMillis = System.currentTimeMillis();

        int miaoShaStatus = 0;
        //还剩多少秒
        int remainSecond = 0;

        if (currentTimeMillis < start) {
            //秒杀活动没有开始 显示倒计时
            miaoShaStatus = 0;
            remainSecond = (int) (currentTimeMillis - start) / 1000;

        } else if (currentTimeMillis > end) {
            //秒杀活动已经结束
            miaoShaStatus = 2;
            remainSecond = -1;

        } else {
            //秒杀活动进行中
            miaoShaStatus = 1;
            remainSecond = 0;
        }
        modelMap.addAttribute("miaoShaStatus", miaoShaStatus);
        modelMap.addAttribute("remainSecond", remainSecond);


        return "goods_detail";
    }


}
